//
//  YZMyStocksTopCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/31.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMyStocksTopCell.h"
#import "YZMAppStyle.h"
#import "YZMStockRealModel.h"
#import "NSNumber+Utils.h"
#import <Masonry/Masonry.h>

@interface YZMyStocksTopCell()

@property (nonatomic, strong) UILabel *leftNameLB;
@property (nonatomic, strong) UILabel *middleNameLB;
@property (nonatomic, strong) UILabel *rightNameLB;

@property (nonatomic, strong) UILabel *leftNumberLB;
@property (nonatomic, strong) UILabel *middleNumberLB;
@property (nonatomic, strong) UILabel *rightNumberLB;

@property (nonatomic, strong) UIImageView *leftArrowIV;
@property (nonatomic, strong) UIImageView *middleArrowIV;
@property (nonatomic, strong) UIImageView *rightArrowIV;

@property (nonatomic, strong) UILabel *leftCombLB;
@property (nonatomic, strong) UILabel *middleCombLB;
@property (nonatomic, strong) UILabel *rightCombLB;
@property(nonatomic, strong) UIView *splitLine;

@property (nonatomic , strong) UILabel* adrLabel;

@end

@implementation YZMyStocksTopCell{
    YZMStockRealModel *_leftRecord;
    YZMStockRealModel *_middleRecord;
    YZMStockRealModel *_rightRecord;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createObjects];
        [self createAdr];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [YZMAppStyle colorForMain];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self createObjects];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [YZMAppStyle colorForMain];
}

- (void)setLeftRecord:(YZMStockRealModel *)leftRecord{
    _leftRecord = leftRecord;
    self.leftNameLB.text = @"上证指数";
    self.leftArrowIV.hidden=NO;
    self.leftNumberLB.hidden=NO;
    self.leftCombLB.hidden=NO;
    
    if (!leftRecord) {
        self.leftArrowIV.hidden=YES;
        self.leftNumberLB.hidden=YES;
        self.leftCombLB.hidden=YES;
    }

        self.leftNumberLB.text  = @(leftRecord.price).p02fString;
        if ([@(leftRecord.price) isEqualGreaterThanDouble:leftRecord.lastClosePrice]) {
            
            self.leftNumberLB.textColor = [YZMAppStyle colorForRiseText];
            
            self.leftCombLB.text = [NSString stringWithFormat:@"+%@  +%@%%",@(leftRecord.price - leftRecord.lastClosePrice).p02fString, @(leftRecord.price/leftRecord.lastClosePrice*100 -100).p02fString];
            self.leftArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
            self.leftCombLB.textColor = [YZMAppStyle colorForRiseText];
        }else{
            
            self.leftNumberLB.textColor = [YZMAppStyle colorForDropText];
            self.leftCombLB.text = [NSString stringWithFormat:@"%@  %@%%",@(leftRecord.price - leftRecord.lastClosePrice).p02fString, @(leftRecord.price/leftRecord.lastClosePrice*100 -100).p02fString];
            self.leftArrowIV.image = [UIImage imageNamed:@"icon_green_down_arrow"];
            self.leftCombLB.textColor = [YZMAppStyle colorForDropText];
    }
    if ([@(leftRecord.price)isEqualWithDouble:0.0]) {
        self.leftNumberLB.textColor = [YZMAppStyle colorForRiseText];
        self.leftCombLB.text = @"0%  0%";
        self.leftArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
        self.leftCombLB.textColor = [YZMAppStyle colorForRiseText];
    }
}

- (void)setMiddleRecord:(YZMStockRealModel *)middleRecord{
    _middleRecord = middleRecord;
    self.middleNameLB.text = @"深证成指";
    
    self.middleCombLB.hidden=NO;
    self.middleNumberLB.hidden=NO;
    self.middleArrowIV.hidden=NO;
    
    if (!middleRecord) {
        self.middleCombLB.hidden=YES;
        self.middleNumberLB.hidden=YES;
        self.middleArrowIV.hidden=YES;
    }
    
    self.middleNumberLB.text = @(middleRecord.price).p02fString;
    
    if ([@(middleRecord.price) isEqualGreaterThanDouble:middleRecord.lastClosePrice]) {
        
        self.middleNumberLB.textColor = [YZMAppStyle colorForRiseText];
        self.middleCombLB.text = [NSString stringWithFormat:@"+%@  +%@%%",@(middleRecord.price - middleRecord.lastClosePrice).p02fString, @(middleRecord.price/middleRecord.lastClosePrice*100 -100).p02fString];
        self.middleArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
        self.middleCombLB.textColor = [YZMAppStyle colorForRiseText];
    }else{
        
        self.middleNumberLB.textColor = [YZMAppStyle colorForDropText];
        self.middleCombLB.text = [NSString stringWithFormat:@"%@  %@%%",@(middleRecord.price - middleRecord.lastClosePrice).p02fString, @(middleRecord.price/middleRecord.lastClosePrice*100 -100).p02fString];
        self.middleArrowIV.image = [UIImage imageNamed:@"icon_green_down_arrow"];
        self.middleCombLB.textColor = [YZMAppStyle colorForDropText];;
    }
    
    if ([@(middleRecord.price)isEqualWithDouble:0.0]) {
        self.middleNumberLB.textColor = [YZMAppStyle colorForRiseText];
        self.middleCombLB.text = @"0%  0%";
        self.middleArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
        self.middleCombLB.textColor = [YZMAppStyle colorForRiseText];
    }
    
    
}

- (void)setRightRecord:(YZMStockRealModel *)rightRecord{
    _rightRecord = rightRecord;
    self.rightNameLB.text = @"创业板指";
    
    self.rightCombLB.hidden=NO;
    self.rightNumberLB.hidden=NO;
    self.rightArrowIV.hidden=NO;
    
    if (!rightRecord) {
        self.rightCombLB.hidden=YES;
        self.rightNumberLB.hidden=YES;
        self.rightArrowIV.hidden=YES;
    }
    
    
    self.rightNumberLB.text = @(rightRecord.price).p02fString;
    
    if ([@(rightRecord.price) isEqualGreaterThanDouble:rightRecord.lastClosePrice]) {
        
        self.rightNumberLB.textColor = [YZMAppStyle colorForRiseText];
        self.rightCombLB.text = [NSString stringWithFormat:@"+%@  +%@%%",@(rightRecord.price - rightRecord.lastClosePrice).p02fString, @(rightRecord.price/rightRecord.lastClosePrice*100 -100).p02fString];
        self.rightArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
        self.rightCombLB.textColor = [YZMAppStyle colorForRiseText];
    }else{
        
        self.rightNumberLB.textColor = [YZMAppStyle colorForDropText];
        self.rightCombLB.text = [NSString stringWithFormat:@"%@  %@%%",@(rightRecord.price - rightRecord.lastClosePrice).p02fString, @(rightRecord.price/rightRecord.lastClosePrice*100 -100).p02fString];
        self.rightArrowIV.image = [UIImage imageNamed:@"icon_green_down_arrow"];
        self.rightCombLB.textColor = [YZMAppStyle colorForDropText];
    }
    
    if ([@(rightRecord.price)isEqualWithDouble:0.0]) {
        self.rightNumberLB.textColor = [YZMAppStyle colorForRiseText];
        self.rightCombLB.text = @"0%  0%";
        self.rightArrowIV.image = [UIImage imageNamed:@"icon_red_top_arrow"];
        self.rightCombLB.textColor = [YZMAppStyle colorForRiseText];
    }

}

- (void)createObjects{
    
    self.leftNameLB = [YZMAppStyle labelWithSmallWhite];
    self.leftNameLB.textAlignment = NSTextAlignmentCenter;
    self.middleNameLB = [YZMAppStyle labelWithSmallWhite];
    self.middleNameLB.textAlignment = NSTextAlignmentCenter;
    self.rightNameLB = [YZMAppStyle labelWithSmallWhite];
    self.rightNameLB.textAlignment = NSTextAlignmentCenter;

    self.leftNumberLB = [UILabel new];
    self.leftNumberLB.textAlignment = NSTextAlignmentCenter;
    self.leftNumberLB.font = [YZMAppStyle largeSizeBoldFont];

    self.middleNumberLB = [UILabel new];
    self.middleNumberLB.textAlignment = NSTextAlignmentCenter;
    self.middleNumberLB.font = [YZMAppStyle largeSizeBoldFont];
    self.rightNumberLB = [UILabel new];
    self.rightNumberLB.textAlignment = NSTextAlignmentCenter;
    self.rightNumberLB.font = [YZMAppStyle largeSizeBoldFont];
    
    self.leftArrowIV = [UIImageView new];
    self.middleArrowIV = [UIImageView new];
    self.rightArrowIV = [UIImageView new];
    
    UIView *leftCombView = [self combViewWithImageView:self.leftArrowIV andLabel:self.leftNumberLB];
    UIView *middleCombView = [self combViewWithImageView:self.middleArrowIV andLabel:self.middleNumberLB];
    UIView *rightCombView = [self combViewWithImageView:self.rightArrowIV andLabel:self.rightNumberLB];
    
    self.leftCombLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    self.leftCombLB.textAlignment = NSTextAlignmentCenter;
    self.middleCombLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    self.middleCombLB.textAlignment = NSTextAlignmentCenter;
    self.rightCombLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    self.rightCombLB.textAlignment = NSTextAlignmentCenter;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.contentView);
        make.width.equalTo(@(ScreenWidth / 3.0));
    }];
    
    [leftButton addTarget:self action:@selector(shangzhengQuotes) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:middleButton];
    [middleButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView.mas_left).with.offset(ScreenWidth / 3.0);
        make.top.bottom.equalTo(self.contentView);
        make.width.equalTo(@(ScreenWidth / 3.0));
    }];
    
    [middleButton addTarget:self action:@selector(shenzhengQuotes) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(ScreenWidth / 3.0 * 2);
        make.top.bottom.equalTo(self.contentView);
        make.width.equalTo(@(ScreenWidth / 3.0));
    }];
    
    [rightButton addTarget:self action:@selector(entrepreneurshipQuotes) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *leftStackView = [self unitViewWithArray:@[self.leftNameLB, leftCombView, self.leftCombLB]];
//    leftStackView.backgroundColor = [UIColor blueColor];
    leftStackView.tag = 1000;
    [YZMSetBorderAndCornerRadius addVerLineViewWithView:leftStackView width:0.5 color:[YZMAppStyle colorForLineOfCell] top:8 bottom:-6];
    leftStackView.userInteractionEnabled = NO;
    [self.contentView addSubview:leftStackView];
    [leftStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView);
        make.width.mas_equalTo(ScreenWidth / 3.0);
    }];
    UIView *middleStackView = [self unitViewWithArray:@[self.middleNameLB, middleCombView, self.middleCombLB]];
    middleStackView.tag = 1001;
    [YZMSetBorderAndCornerRadius addVerLineViewWithView:middleStackView width:0.5 color:[YZMAppStyle colorForLineOfCell] top:8 bottom:-6];
    middleStackView.userInteractionEnabled = NO;
    [self.contentView addSubview:middleStackView];
    [middleStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftStackView.mas_top);
        make.left.equalTo(leftStackView.mas_right);
        make.width.mas_equalTo(ScreenWidth / 3.0);
    }];
    UIView *rightStackView = [self unitViewWithArray:@[self.rightNameLB, rightCombView, self.rightCombLB]];
    rightStackView.tag = 1002;
    rightStackView.userInteractionEnabled = NO;
    [self.contentView addSubview:rightStackView];
    [rightStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(middleStackView.mas_top);
        make.left.equalTo(middleStackView.mas_right);
        make.width.mas_equalTo(ScreenWidth / 3.0);
    }];
}

- (void)createAdr {
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 1004;
    [btn addTarget:self action:@selector(discovery) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView.mas_bottom).offset(-40);
    }];
    
    UIImageView* imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"notice"];
    [btn addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_left).offset(15);
        make.top.equalTo(btn.mas_top).offset(10);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(14);
    }];
    
    UILabel* label = [UILabel new];
    label.textColor = [UIColor whiteColor];
    label.text = @"公告:";
    label.font = [YZMAppStyle smallSizeFont];
    [btn addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(5);
        make.centerY.equalTo(imageView);
        make.width.mas_equalTo(35);
    }];

    _adrLabel = [UILabel new];
    _adrLabel.text = @"广告";
    _adrLabel.font = [YZMAppStyle smallSizeFont];
    _adrLabel.textColor = [YZMAppStyle colorForLightGrayText];
    [btn addSubview:_adrLabel];
    [_adrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(5);
        make.centerY.equalTo(imageView);
        make.right.equalTo(btn.mas_right).offset(-10);
        
    }];
    
    UIView* view = [UIView new];
    view.backgroundColor = [YZMAppStyle colorForLineOfCell];
    [self.contentView addSubview:view];
    view.tag = 1005;
    view.hidden = YES;
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_left).offset(15);
        make.top.equalTo(btn.mas_top).offset(-3);
        make.right.equalTo(btn.mas_right).offset(-15);
        make.height.mas_equalTo(1);
    }];
    
    btn.hidden = YES;
}

// 上证指数
- (void)shangzhengQuotes{
    if (_leftRecord == nil) {
        _leftRecord = [[YZMStockRealModel alloc] init];
        _leftRecord.code = @"SH000001";
        _leftRecord.name = @"上证指数";
    }
    [YZMCMInstance pushViewControllerWithName:@"threeQutoes" andParams:@{@"code":_leftRecord.code,@"name":_leftRecord.name}];
}

//深证成指
- (void)shenzhengQuotes{
    if (_middleRecord == nil) {
        _middleRecord = [[YZMStockRealModel alloc] init];
        _middleRecord.code = @"SZ399001";
        _middleRecord.name = @"深证成指";
    }
    [YZMCMInstance pushViewControllerWithName:@"threeQutoes" andParams:@{@"code":_middleRecord.code,@"name":_middleRecord.name}];
}

//创业板指
- (void)entrepreneurshipQuotes{
    if (_rightRecord == nil) {
        _rightRecord = [[YZMStockRealModel alloc] init];
        _rightRecord.code = @"SZ399006";
        _rightRecord.name = @"创业板指";
    }
    [YZMCMInstance pushViewControllerWithName:@"threeQutoes" andParams:@{@"code":_rightRecord.code,@"name":_rightRecord.name}];
}


- (UIView *)unitViewWithArray:(NSArray *)array{
    
    UIView *view = [UIView new];
    UIView *lastView = nil;
    NSInteger i = - 1;
    for (UIView *subView in array) {
//        subView.backgroundColor = [YZMAppStyle ]
        i++;
        [view addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 2) {
                make.centerX.equalTo(lastView);
            }else{
                make.left.centerX.equalTo(view);
            }
            
            make.width.lessThanOrEqualTo(view);
            if (lastView) {
                make.top.equalTo(lastView.mas_bottom).offset(8);
            }else{
                make.top.equalTo(view);
            }
        }];
        
        lastView = subView;
    }
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView);
    }];
    
    return view;
}

- (UIView *)combViewWithImageView:(UIImageView *)imageView andLabel:(UILabel *)label{
    
    UIView *view = [UIView new];
    
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(view);
        make.centerX.equalTo(view.mas_centerX).offset(6);
    }];
    
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.width.equalTo(@6);
        make.height.equalTo(@4);
        make.right.equalTo(label.mas_left).offset(-5);
    }];
    
    return view;
}

- (void)setAdr:(NSString *)adr {
    _adr = adr;
    
    for (NSInteger i = 1000; i < 1003; i++) {
        UIView *iv = [self.contentView viewWithTag:i];
        [iv mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo((ScreenWidth - 20) / 3.0);
        }];
    }
    
    UIButton* btn = [self.contentView viewWithTag:1004];
    btn.hidden = NO;
    _adrLabel.text = adr;
    
    UIView* view = [self.contentView viewWithTag:1005];
    view.hidden = NO;
}

- (void)discovery {
    [[YZMControllerManager sharedManager] pushViewControllerWithName:YZDiscoveryCtrl andParams:nil];
}

@end
